<?php
// +----------------------------------------------------------------------
// |ZBPHP[基于ThinkPHP5.1开发]
// +----------------------------------------------------------------------
// | Copyright (c) 2016-2019 http://www.benbenwangluo.com
// +----------------------------------------------------------------------
// | Author 似水星辰 [2630481389@qq.com]
// +----------------------------------------------------------------------
// | 中犇软件 技术六部 出品
// +----------------------------------------------------------------------

namespace app\order\admin;

use app\admin\admin\Base;
use app\order\model\ExpressCompany as ExpressCompanyModel;
use service\Format;

/**
 * 快递公司控制器
 * @package app\ExpressCompany\admin
 */
class ExpressCompany extends Base
{
    /**
     * 快递公司列表
     * @return mixed
     * @author 似水星辰 [2630481389@qq.com]
     */
    public function index()
    {
        cookie('__forward__', $_SERVER['REQUEST_URI']);
        // 查询
        // 查询
        $map = [];
        $param = $this->request->param();
        if($param["name"]){
            $map[] = ["name","like","%".$param["name"]."%"];
        }
        if($param["express_no"]){
            $map[] = ["express_no","like","%".$param["express_no"]."%"];
        }
        // 排序
        $order = $this->getOrder("sort asc,express_no asc,aid desc");
        
        // 数据列表
        unset($map['export']);
        $data_list = ExpressCompanyModel::where($map)
            ->where('is_delete', 0)
            ->order($order)->paginate();
        $fields = [
            ['aid', 'ID', '', '', '',],
            ['name', lang('公司名称'), '', '', '',],
            ['express_no', lang('公司编号'), '', '', '',],
            ['tel', lang('联系电话'), '', '', '',],
            ['logo', lang('公司') . 'LOGO', 'picture'],
            ['sort', lang('排序'), 'text.edit', '0', '',],
            ['status', lang('状态'), 'status'],
            ['right_button', lang('操作'), 'btn', '', '', 'text-center']
        ];
        $searchFields = [
            ['name', lang('公司名称'), 'text'],
            ['express_no', lang('公司编号'), 'text'],
        ];
        return Format::ins()//实例化
            ->setPageTips("添加快递公司不可删除，这是基层配置，请谨慎操作，系统预置公司未上传LOGO，请上传LOGO")       
            ->setPrimaryKey('aid')
            ->addColumns($fields)//设置字段
            ->setTopSearch($searchFields)
            ->setTopButtons([
                    ['ident'=> 'add', 'title'=>'新增', 'href'=>'add', 'icon'=>'fa fa-plus pr5','class'=>'btn btn-sm mr5 btn-primary layeradd '],
                    ['ident'=> 'enable', 'title'=>'批量启用','href'=>['setstatus',['type'=>'enable']],'icon'=>'fa fa-check-circle pr5','class'=>'btn btn-sm mr5 btn-default  ajax-post confirm','extra'=>'target-form="ids"'],
                    ['ident'=> 'disable', 'title'=>'批量禁用','href'=>['setstatus',['type'=>'disable']],'icon'=>'fa fa-ban pr5','class'=>'btn btn-sm mr5 btn-default  ajax-post confirm','extra'=>'target-form="ids"'],
             ])
            ->setRightButtons($this->right_button_layer,["delete"])
            ->setData($data_list)//设置数据
            ->fetch();//显示
    }

    /**
     * 新增
     * @return mixed
     * @author 似水星辰 [2630481389@qq.com]
     */
    public function add()
    {
        // 保存数据
        if ($this->request->isPost()) {
            // 表单数据
            $data = $this->request->post();

            // 验证
            $result = $this->validate($data, 'ExpressCompany');
            if (true !== $result) {
                $this->error($result);
            }

            if ($page = ExpressCompanyModel::create($data)) {
                $this->success(lang('新增成功'), cookie('__forward__'));
            } else {
                $this->error(lang('新增失败'));
            }
        }

        $fields = [
            ['type' => 'text', 'name' => 'name', 'title' => lang('公司名称'),],
            ['type' => 'text', 'name' => 'express_no', 'title' => lang('公司编号'), 'tips' => lang('请对照') . ' <a target="_blank" href="' . url('express_code') . '">' . lang('物流公司编码表') . '</a>' . lang('输入对应快递查询的物流公司编码')],
            ['type' => 'text', 'name' => 'tel', 'title' => lang('联系电话'),],
            ['type' => 'image', 'name' => 'logo', 'title' => lang('公司') . 'LOGO'],
            ['type' => 'number', 'name' => 'sort', 'title' => lang('排序'), 'value' => '100',],
            ['type' => 'radio', 'name' => 'is_default', 'title' => lang('是否设置默认'), 'value' => '0', 'extra' => [lang('否'), lang('是')]]

        ];
        $this->assign('page_title', lang('新增快递公司'));
        $this->assign('form_items', $fields);
        return $this->fetch('admin@public/add');
    }

    /**
     * 编辑
     * @param null $id 快递公司id
     * @return mixed
     * @author 似水星辰 [2630481389@qq.com]
     */
    public function edit($id = null)
    {
        if ($id === null) {
            $this->error(lang('缺少参数'));
        }

        // 保存数据
        if ($this->request->isPost()) {
            // 表单数据
            $data = $this->request->post();
            $data['update_time'] = time();

            // 验证
            $result = $this->validate($data, 'ExpressCompany');
            if (true !== $result) {
                $this->error($result);
            }

            if (ExpressCompanyModel::update($data)) {
                $this->success(lang('编辑成功'), cookie('__forward__'));
            } else {
                $this->error(lang('编辑失败'));
            }
        }

        $info = ExpressCompanyModel::get(['aid' => $id]);
        $fields = [
            ['type' => 'hidden', 'name' => 'aid'],
            ['type' => 'text', 'name' => 'name', 'title' => lang('公司名称')],
            ['type' => 'text', 'name' => 'express_no', 'title' => lang('公司编号')],
            ['type' => 'text', 'name' => 'tel', 'title' => lang('联系电话')],
            ['type' => 'image', 'name' => 'logo', 'title' => lang('公司') . 'LOGO'],
            ['type' => 'number', 'name' => 'sort', 'title' => lang('排序')],
            ['type' => 'radio', 'name' => 'is_default', 'title' => lang('是否设置默认'), 'extra' => [lang('否'), lang('是')]]

        ];
        $this->assign('page_title', lang('编辑快递公司'));
        $this->assign('form_items', $this->setData($fields, $info));
        return $this->fetch('admin@public/edit');
    }

    /**
     * 查看快递编码
     * @author 刘明美 [ liumingmei@zhongbenjituan.com ]
     * @created 2019/10/28 16:58
     */
    public function express_code()
    {
        return $this->fetch();
    }

    //快递公司删除改成软删除
    public function delete($ids)
    {
    
    }
}
